<script setup>
import { UseMenuStore, UseUserStore } from "../store/index";
const usmenustore = UseMenuStore();
const useuserStore = UseUserStore();

// 获取在本地存储中的头像信息
const pz_avatar = useuserStore.pz_userInfo.avatar;
const pz_name = useuserStore.pz_userInfo.name;

// 获取路由的地址栏地址
import { useRoute, useRouter } from "vue-router";
const route = useRoute();
const router = useRouter();
// console.log(route.path);

// 点击关闭tab标签
const closeTab = (item, index) => {
  // console.log(index);
  // console.log(usmenustore.selectMenu.length);

  // 如果删除tab非当前高亮tab栏
  if (route.path !== item.path) {
    return;
  }
  usmenustore.closeMenu(item);
  // 删除高亮tab栏
  // 如果删除最后一项
  if (index === usmenustore.selectMenu.length) {
    //  如果剩下最后一个
    if (usmenustore.selectMenu.length === 0) {
      router.push("/");
    } else {
      router.push({ path: usmenustore.selectMenu[index - 1].path });
    }
  } else {
    // 删除的是中间高亮tab栏
    router.push({ path: usmenustore.selectMenu[index].path });
  }
};

const handleClick = (cancel) => {
  if (cancel === "cancel") {
    // 清除当前域名下的所有 Local Storage 数据
    localStorage.clear();
    // 跳转路由
    window.location.href = window.location.origin;
    // router.push("/login");
  }
};
</script>

<template>
  <div class="header-container">
    <div class="header-left flex-box">
      <el-icon class="icon" size="20" @click="usmenustore.collapseMenu"
        ><Fold
      /></el-icon>
      <!-- 渲染tab栏 -->
      <ul class="flex-box">
        <li
          v-for="(item, index) in usmenustore.selectMenu"
          :class="{ selected: route.path === item.path }"
          :key="item.path"
          class="tab flex-box"
        >
          <el-icon size="12">
            <component :is="item.icon"></component>
          </el-icon>
          <RouterLink :to="{ path: item.path }" class="flex-box text">
            {{ item.name }}
          </RouterLink>
          <el-icon size="12" class="close" @click="closeTab(item, index)"
            ><Close
          /></el-icon>
        </li>
      </ul>
    </div>
    <div class="header-right flex-box">
      <el-dropdown @command="handleClick">
        <div class="el-dropdown-link flex-box">
          <el-avatar class="mr-3" :size="32" :src="pz_avatar" />
          <p class="user-name">{{ pz_name }}</p>
        </div>

        <template #dropdown>
          <el-dropdown-item command="cancel">退出登录</el-dropdown-item>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<style scoped lang="less">
.flex-box {
  display: flex;
  align-items: center;
  height: 100%;
}
.header-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  background-color: #fff;
  padding-right: 25px;
  .header-left {
    height: 100%;
    .icon {
      width: 45px;
      height: 100%;
    }
    .icon:hover {
      background-color: #f5f5f5;
      cursor: pointer;
    }
    .tab {
      padding: 0 10px;
      height: 100%;
      .text {
        margin: 0 5px;
      }
      .close {
        visibility: hidden;
      }
      &.selected {
        a {
          color: #409eff;
        }
        i {
          color: #409eff;
        }
        background-color: #f5f5f5;
      }
    }
    .tab:hover {
      background-color: #f5f5f5;
      .close {
        visibility: inherit;
        cursor: pointer;
        color: #333;
      }
    }
  }
  .header-right {
    .user-name {
      margin-left: 10px;
    }
  }
}
a {
  height: 100%;
  color: #333;
  font-size: 15px;
}
</style>
